﻿using Iworld.Author.Model;
using Iworld.Basis.Help;
using Iworld.Basis.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Manager
{
    /// <summary>
    ///金币帐变记录的管理者对象
    /// </summary>
    [RegisterToManagerService]
    public class GoldChangeRecordManager : ManagerBase<IModelToDbContextOfAuthor, GoldChangeRecordManager.Actions,GoldChangeRecord>
    {
        public GoldChangeRecordManager(IModelToDbContextOfAuthor db)
            : base(db)
        {}

        /// <summary>
        /// 服务：生成对应的帐变记录
        /// </summary>
        /// <param name="info">数据集</param>
        [OnCall(typeof(GoldChangeRecordManager),Services.CreateGoldChangeRecord)]
        public static void ServiceForCreateGoldChangeRecord(InfoOfCallOnManagerService info)
        {
            CreateGoldChangeRecordArgs args = (CreateGoldChangeRecordArgs)info.Args;
         /*   string lk =args.UserId.ToString();
            lock(lk)
            {*/
                IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
                Iworld.Author.Model.Author user = db.Authors.FirstOrDefault(x => x.Id == args.UserId);
                if (user == null)
                {
                    throw new Exception("用户不存在");
                }
                GoldChangeRecord gr = new GoldChangeRecord(user, args.Type, args.Description, args.Income, args.Expenses);
                GoldChangeRecordManager manager = new GoldChangeRecordManager(db);
                manager.OnExecuting(Actions.Create, gr);
                db.GoldChangeRecords.Add(gr);
                manager.OnExecuted(Actions.Create, gr);
           /* }*/
        }

        public override GoldChangeRecord Create(IPackageForCreateModel<IModelToDbContextOfAuthor, GoldChangeRecord> package)
        {
            throw new Exception("请使用统一服务方法创建帐变记录");
        }

        /// <summary>
        /// 监听：在用户被移除之前，移除所有相关的记录
        /// </summary>
        /// <param name="info">数据集</param>
        [Listen(typeof(AuthorManager), AuthorManager.Actions.Remove, ExecutionOrder.Before)]
        public static void Monitor_RemoveRecordsWhenUserRemoving(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            Model.Author model = (Model.Author)info.Model;
            db.GoldChangeRecords.Where(x => x.Owner.Id == model.Id).ToList()
                .ForEach(x =>
                {
                    db.GoldChangeRecords.Remove(x);
                });
        }

        /// <summary>
        /// 监听：申请提现
        /// </summary>
        /// <param name="info">数据集</param>
        [Listen(typeof(WithdrawalsRecordManager), WithdrawalsRecordManager.Actions.Create, ExecutionOrder.After)]
        public static void MonitorCreateWhenWithdrawals(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            WithdrawalsRecord model = (WithdrawalsRecord)info.Model;
            GoldChangeRecordManager manager = new GoldChangeRecordManager(db);

            GoldChangeRecord record = new GoldChangeRecord(model.Owner
                , "提现"
                , model.Status.ToString()
                , 0
                , model.Golds);
            manager.OnExecuting(Actions.Create, record);
            db.GoldChangeRecords.Add(record);
            manager.OnExecuted(Actions.Create, record);
        }

        /// <summary>
        /// 监听：提现失败
        /// </summary>
        /// <param name="info">数据集</param>
        [Listen(typeof(WithdrawalsRecordManager), WithdrawalsRecordManager.Actions.ChangeStatus, ExecutionOrder.After)]
        public static void MonitorCreateWhenLostWithdrawals(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            WithdrawalsRecord model = (WithdrawalsRecord)info.Model;

            if (model.Status == WithdrawalsStatus.失败)
            {
                GoldChangeRecordManager manager = new GoldChangeRecordManager(db);
                GoldChangeRecord record = new GoldChangeRecord(model.Owner
                    , "提现失败"
                    , model.Status.ToString()
                    , model.Golds
                    , 0);
                manager.OnExecuting(Actions.Create, record);
                db.GoldChangeRecords.Add(record);
                manager.OnExecuted(Actions.Create, record);
            }
        }

        /// <summary>
        /// 监听充值成功
        /// </summary>
        /// <param name="info"></param>
        [Listen(typeof(RechargeCardManager), RechargeCardManager.Actions.Recharge,ExecutionOrder.After)]
        public static void ListenRecharge(InfoOfSendOnManagerService info)
        {
            RechargeCard m = (RechargeCard)info.Model;
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            CreateGoldChangeRecordArgs args = new CreateGoldChangeRecordArgs()
            {
                Income = m.Golds,
                UserId = m.UsedAuthor.Id,
                Type = "充值"
            };
             InfoOfCallOnManagerService callInfo =
                 new InfoOfCallOnManagerService(db, typeof(GoldChangeRecordManager), Services.CreateGoldChangeRecord, args);
             ManagerService.Call(callInfo);
        }
        
        /// <summary>
        /// 监听赠送金币
        /// </summary>
        /// <param name="info"></param>
        [Listen(typeof(GiveGoldsRecordManager), GiveGoldsRecordManager.Actions.Create,ExecutionOrder.After)]
        public static void ListenGiveGolds(InfoOfSendOnManagerService info)
        {
            GiveGoldsRecord model = (GiveGoldsRecord)info.Model;
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            CreateGoldChangeRecordArgs to = new CreateGoldChangeRecordArgs()
            {    
                Income =model.Golds,
                UserId = model.To.Id,
                Type = "被赠送"
            };
            CreateGoldChangeRecordArgs from = new CreateGoldChangeRecordArgs()
            {
                Expenses = model.Golds,
                UserId = model.From.Id,
                Type = "赠送"
            };
            InfoOfCallOnManagerService infoTo = new InfoOfCallOnManagerService(db, typeof(GoldChangeRecordManager), Services.CreateGoldChangeRecord, to);
            InfoOfCallOnManagerService infoFrom = new InfoOfCallOnManagerService(db, typeof(GoldChangeRecordManager), Services.CreateGoldChangeRecord, from);
            ManagerService.Call(infoTo);
            ManagerService.Call(infoFrom);
        }

 
        #region 枚举

        /// <summary>
        /// 动作
        /// </summary>
        public enum Actions
        {
            /// <summary>
            /// 创建新的金币帐变记录
            /// </summary>
            Create,
            /// <summary>
            /// 修改金币帐变记录
            /// </summary>
            Update,
            /// <summary>
            /// 移除金币帐变记录
            /// </summary>
            Remove,
           
        }

        public enum Services
        {
            CreateGoldChangeRecord
        }
 

        #endregion


        /// <summary>
        /// 用于给生成对应的帐变记录的服务提供数据的数据集
        /// </summary>
        public class CreateGoldChangeRecordArgs
        {
            /// <summary>
            /// 用户的存储指针
            /// </summary>
            public int UserId { get; set; }

            /// <summary>
            /// 帐变类型
            /// </summary>
            public string Type { get; set; }

            /// <summary>
            /// 描述
            /// </summary>
            public string Description { get; set; }

            /// <summary>
            /// 收入
            /// </summary>
            public double Income { get; set; }

            /// <summary>
            /// 支出
            /// </summary>
            public double Expenses { get; set; }
        }
 


        #region 接口
      
        #endregion
       

    }
}
